package LeetCode.LeetCode2;

/**
 * 有序数组的平方
 * @author lumin
 * @date 2022/03/17 13:28
 **/
public class Num977 {
    public int[] sortedSquares(int[] nums) {
        if(nums.length == 1){
            nums[0] *= nums[0];
            return nums;
        }
        for (int i = 0; i < nums.length; i++) {
            nums[i] *= nums[i];
        }
        int low = 0;
        int hight = nums.length - 1;
        while(low <= hight){
            int max = low;
            int min = low;
            for (int i = low; i <= hight; i++) {
                if(nums[i] > nums[max]){
                    max = i;
                }
                if(nums[i] < nums[min]){
                    min = i;
                }
            }
            swap(nums,low,min);
            if(max == low){
                //最大值已经被换到min
                max = min;
            }
            swap(nums,max,hight);
            low++;
            hight--;
        }
        return nums;
    }

    public static void swap(int[] arr,int i ,int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}